package com.woniu.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.product.entity.ChargePileQuery;
import com.woniu.product.entity.ChargePileStatistics;
import com.woniu.product.pojo.ChargePile;
import com.woniu.product.mapper.ChargePileMapper;
import com.woniu.product.service.IChargePileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 充电站表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-20
 */
@Service
public class ChargePileServiceImpl extends ServiceImpl<ChargePileMapper, ChargePile> implements IChargePileService {

    @Autowired(required = false)
    private ChargePileMapper chargePileMapper;


    @Override
    public void addChargePile(ChargePile chargePile) {
        chargePileMapper.insert(chargePile);
    }

    @Override
    public void deleteChargePile(String id) {
        chargePileMapper.deleteById(id);
    }

    @Override
    public void updateChargePile(ChargePile chargePile) {
        chargePileMapper.updateById(chargePile);
    }

    @Override
    public PageResult<ChargePile> queryChargePile(Integer currentPage, Integer pageSize, ChargePileQuery chargePileQuery) {
        IPage<ChargePile> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<ChargePile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(chargePileQuery.getId()), ChargePile::getId , chargePileQuery.getId())
                .eq(!StringUtils.isEmpty(chargePileQuery.getStatus()) , ChargePile::getStatus , chargePileQuery.getStatus())
                .gt(!StringUtils.isEmpty(chargePileQuery.getStartTime()) , ChargePile::getCreatedTime , chargePileQuery.getStartTime())
                .lt(!StringUtils.isEmpty(chargePileQuery.getEndTime()) , ChargePile::getCreatedTime , chargePileQuery.getEndTime());
        IPage<ChargePile> selectPage = chargePileMapper.selectPage(page, wrapper);
        return new PageResult<ChargePile>(selectPage.getTotal(),selectPage.getRecords());
    }

    @Override
    public PageResult<ChargePileStatistics> queryChargePileStatistics(Integer currentPage, Integer pageSize, ChargePileQuery chargePileQuery) {
        IPage<ChargePileStatistics> page = new Page<>(currentPage,pageSize);
        Page<ChargePileStatistics> pileStatisticsPage = chargePileMapper.queryChargePileStatistics(page,chargePileQuery);
        for (ChargePileStatistics pileStatistics : pileStatisticsPage.getRecords()) {
            System.out.println(">>>>>>>>>>>" + pileStatistics);
            Long startTime = chargePileQuery.getStartTime().getTime();
            Long endTime = chargePileQuery.getEndTime().getTime();
            Long diffTime = (endTime - startTime) / (1000 * 60);
            double v = diffTime.doubleValue();
            Integer usageDuration = pileStatistics.getUsageDuration();
            Double u = ((usageDuration)/ v ) * 100;
            String usageEfficiency = String.format("%.2f", u) + " %";
            pileStatistics.setUsageEfficiency(usageEfficiency);
        }
        return new PageResult<ChargePileStatistics>(pileStatisticsPage.getTotal(),pileStatisticsPage.getRecords());
    }


}
